﻿using System.Web.Mvc;
using HelloWorld.Code.DataAccess;
using HelloWorld.Code.Security;
using UserRole = HelloWorld.Code.Security.UserRole;

namespace HelloWorld.Controllers
{
    [UserRoleAuthorize(UserRole.Administrator, UserRole.User)]
    public class HomeController : Controller
    {
        public ActionResult Index(string id)
        {
            ViewData["Message"] = "This is a string created in the controller";
            ViewBag.Id = id;
            return View();
        }

        [HttpGet]
        public ActionResult ContactUs()
        {
            return View(new Code.DataAccess.Message());
        }
        
        [HttpPost]
        public ActionResult ContactUs(Message message)
        {
            #region It is possible to add model validation inside the controller BUT in most of the cases this is not a good idea (so we removed it)

            ////Name
            //if(string.IsNullOrEmpty(message.Name))
            //    ModelState.AddModelError("Name", "Required");
            //else if(message.Name.Length <3 || message.Name.Length > 50)
            //    ModelState.AddModelError("Name", "The name must be a string of at least 3 characters and at most 50 characters");

            ////Email
            //if (string.IsNullOrEmpty(message.Email))
            //    ModelState.AddModelError("Email", "Required");
            //else if (message.Email.Length > 50)
            //    ModelState.AddModelError("Email", "The e-mail can't have more than 50 characters");
            //else if (!Regex.IsMatch(message.Email, @"^\w+([-+.]*[\w-]+)*@(\w+([-.]?\w+)){1,}\.\w{2,4}$"))
            //    ModelState.AddModelError("Email", "Invalid e-mail");

            ////ContactReasonId
            //if(!message.ContactReasonId.HasValue)
            //    ModelState.AddModelError("ContactReasonId", "Required");

            ////Subject
            //if (string.IsNullOrEmpty(message.Subject))
            //    ModelState.AddModelError("Subject", "Required");
            //else if (message.Subject.Length > 100)
            //    ModelState.AddModelError("Subject", "The subject must be a string with a maximum length of 100 characters");

            ////Message
            //if (string.IsNullOrEmpty(message.Message1))
            //    ModelState.AddModelError("Message1", "Required");

            #endregion

            if (ModelState.IsValid)
            {
                string saveResultMessage;
                ViewBag.SaveResult = message.Save(out saveResultMessage);
                ViewBag.SaveResultMessage = saveResultMessage;
            }
            return View(message);
        }

        public JsonResult ValidateEmail(string email)
        {
            string message;
            return Message.EmailAlradyExists(email, out message)
                       ? Json(message, JsonRequestBehavior.AllowGet)
                       : Json(true, JsonRequestBehavior.AllowGet);
        }
    }
}
